<?php

class Keywords{
	
	/**
	 * 添加
	 *
	 */
	function add($plantform_id,$url,$title,$keywords,$description,$remark,$disabled,$sort){
		
		//管理员的id
		import("@.ORG.Admin");
		$cAdmin = new Admin();
    	
		//登录当前管理员
		$admin = $cAdmin->logged_info();
   		$admin_id = $admin['admin_id'];
   		
		$data = array(			
			'plantform_id'	=> $plantform_id,
			'url'			=> $url,
			'title'			=> $title,
			'keywords'		=> $keywords,
			'description'  	=> $description,
            'remark'        => $remark,
			'admin_id'  	=> $admin_id,
			'disabled'		=> $disabled,
            'sort'          => $sort
		);
		
		$mKeywords = M('Keywords');
		//返回自增id
		$id = $mKeywords->add($data);
		
		return $id;
	}
	
	/**
	 * 修改
	 *
	 * @param unknown_type $id
	 * @param unknown_type $upItems
	 * @return unknown
	 */
	function update($id,$upItems){
		$mKeywords = M('Keywords');
		return $mKeywords -> where('id='.$id)->save($upItems);
	}
	
	function get_by_id($id){
		$mKeywords = M('Keywords');
		return $mKeywords -> getById($id);
	}
	
	function get_by_url($url){
		$mKeywords = M('Keywords');
		return $mKeywords -> getByUrl($url);
	}
	
	//获取关键词
	function get_keywords(){
		
		$request_uri = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI']:'';
		
		$keywords = $this->lists();
		
		foreach($keywords as $i => $k){
			if($k['url'] == $request_uri){
				return $k;
			}
		}
		
		return false;
	}

    function list_all($where,$page,$pageSize,&$allCount){

        $whereSql = ' 1 = 1 ';
        if(!is_null($where)){

            if(isset($where['plantform_id'])){

                $whereSql .= " AND keywords.plantform_id = {$where['plantform_id']} ";

                unset($where['plantform_id']);
            }

            foreach ($where as $k => $v){
                if($k != '' && trim($v) != ''){
                    $whereSql .= " AND {$k} LIKE '%{$v}%' ";
                }
            }
        }

        //开始行
        $startIndex = ($page-1) * $pageSize;

        //总计数
        $allCount = $this->lists_count($whereSql);

        $mKeywords = M('Keywords');
        return $mKeywords
            ->join('admin AS a ON a.id = keywords.admin_id')
            ->join('plantform AS p ON p.plantform_id = keywords.plantform_id')
            ->field('keywords.*,a.real_name,p.plantform_name')
            ->order('sort ASC,id ASC')
            ->where($whereSql)
            ->limit($startIndex. ',' . $pageSize)
            ->select();
    }

    function lists_count($whereSql){

        $mKeywords = M('Keywords');
        return $mKeywords
            ->join('admin AS a ON a.id = keywords.admin_id')
            ->join('plantform AS p ON p.plantform_id = keywords.plantform_id')
            ->where($whereSql)
            ->getField('COUNT(*)');
    }


    function lists(){
		$mKeywords = M('Keywords');
		return $mKeywords
		->join('admin AS a ON a.id = keywords.admin_id')
		->join('plantform AS p ON p.plantform_id = keywords.plantform_id')
		->field('keywords.*,a.real_name,p.plantform_name')
		->order('sort ASC,id ASC')
		->select();
	}
	
	//删除
	function delete_by_id($id){
		$m = M('Keywords');
		$delete_result = $m -> where('id='.$id)->delete();
		return $delete_result;
	}
	
	/**
	 * 
	 * 按平台获取最早未使用过的会员
	 * @param unknown_type $plantform_id
	 */
	function get_by_plantform_id($plantform_id,$number =null){
		
		if(is_null($number)){
			$number = PHP_INT_MAX;
		}
		
		$m = M('Keywords');
		return $m->table('keywords')
		->where('plantform_id='.$plantform_id.' AND disabled = 0')
		->order('sort ASC,last_use_time ASC,id ASC')
		->limit('0,' . $number)
		->select();
	}
	
	/**
	 * 
	 * 是否已存在
	 * @param unknown_type $plantform_id
	 * @param unknown_type $url
	 */
	function exist($plantform_id,$url){
		$m = M('Keywords');
		return $m->table('keywords')
		->where("plantform_id=$plantform_id AND url = '$url'")
		->find();
		
	}

    /**
     * 批量修改启用禁用状态
     * @param $keywords_id
     * @param $disabled
     * @return mixed
     */
    function batch_update_disabled($keywords_id,$disabled){

        $upItems = array('disabled' => $disabled);

        $m = M('Keywords');
        return $m->table('keywords')
            ->where("id IN ($keywords_id)")->save($upItems);
    }
}